package org.ohos.rajawali3d.examples.examples.postprocessing;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation;
import org.rajawali3d.animation.Animation3D;
import org.rajawali3d.animation.RotateOnAxisAnimation;
import org.rajawali3d.loader.LoaderAWD;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.postprocessing.PostProcessingManager;
import org.rajawali3d.postprocessing.passes.FXAAPass;
import org.rajawali3d.postprocessing.passes.RenderPass;

public class FXAAFragment extends AExampleFragment {

    private boolean fxaaEnabled = true;

    @Override
    public AExampleRenderer createRenderer() {
        return new FXAARenderer(getContext(), this);
    }

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        super.onCreateView(inflater, container, savedInstanceState);
        inflater.parse(ResourceTable.Layout_fxaa_button_bar, mLayout, true);

        final Button button = (Button) mLayout.findComponentById(ResourceTable.Id_button1);
        button.setText(ResourceTable.String_fxaa_fragment_button_disable_fxaa);
        button.setTextSize(40);
        button.setTextAlignment(TextAlignment.CENTER);
        button.setHeight(100);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                fxaaEnabled = !fxaaEnabled;
                if (fxaaEnabled) {
                    ((FXAARenderer) mRenderer).enableFXAA();
                    button.setText(ResourceTable.String_fxaa_fragment_button_disable_fxaa);
                } else {
                    ((FXAARenderer) mRenderer).disableFXAA();
                    button.setText(ResourceTable.String_fxaa_fragment_button_enable_fxaa);
                }
            }
        });
        mLayout.moveChildToFront(mLayout.findComponentById(ResourceTable.Id_button_group));
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        return mLayout;
    }

    private final class FXAARenderer extends AExampleRenderer {
        private PostProcessingManager mEffects;
        private RenderPass mRenderPass;
        private FXAAPass mFXAAPass;

        FXAARenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            try {
                ResourceManager resManager = mContext.getResourceManager();
                final LoaderAWD parser = new LoaderAWD(mContext.getResourceManager(), mTextureManager,
                        resManager.getRawFileEntry("resources/rawfile/awd_arrows.awd"));
                parser.parse(mContext);

                final Object3D obj = parser.getParsedObject();

                obj.setScale(0.25f);
                getCurrentScene().addChild(obj);

                final Animation3D anim = new RotateOnAxisAnimation(Vector3.Axis.Y, -360);
                anim.setDurationDelta(4d);
                anim.setRepeatMode(Animation.RepeatMode.INFINITE);
                anim.setTransformable3D(obj);
                //anim.play();
                //getCurrentScene().registerAnimation(anim);

                //
                // -- Create a post processing manager. We can add multiple passes to this.
                //

                mEffects = new PostProcessingManager(this, 1.3333);
                mRenderPass = new RenderPass(getCurrentScene(), getCurrentCamera(), 0);
                mEffects.addPass(mRenderPass);

                mFXAAPass = new FXAAPass();
                mRenderPass.setRenderToScreen(true);
                mEffects.addPass(mFXAAPass);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onRender(final long elapsedTime, final double deltaTime) {
            mEffects.render(elapsedTime, deltaTime);
        }

        public void enableFXAA() {
            mRenderPass.setRenderToScreen(true);
            mFXAAPass.setRenderToScreen(false);
            mEffects.addPass(mFXAAPass);
        }

        public void disableFXAA() {
            mFXAAPass.setRenderToScreen(false);
            mRenderPass.setRenderToScreen(true);
            mEffects.removePass(mFXAAPass);
        }
    }

}
